package com.solr;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @program: Solr
 * @description:
 * @author: Dawson.Zhang
 * @create: 2020-03-17
 **/
public class Solrfuxi2 {
//            "product_catalog_name": "幽默杂货",
//            "product_price": 18.9,
//            "product_name": "花儿朵朵彩色金属门后挂 8钩免钉门背挂钩2066",
//            "id": "1",
//            "product_picture": "2014032613103438.png",
    private static final String BASE_URL = "http://192.168.117.131:8080/solr/collection1";

    public static HttpSolrClient getClient() {
        return new HttpSolrClient.Builder(BASE_URL).build();
    }
    @Test
    public void addDoc() throws Exception{
        HttpSolrClient client = getClient();
        SolrInputDocument solrInputFields = new SolrInputDocument();
        solrInputFields.addField("product_catalog_name","生活百货");
        solrInputFields.addField("product_price","124");
        solrInputFields.addField("product_name","生活就像一场戏，戏如人生，人生如戏");
        solrInputFields.addField("id","10086");
        client.add(solrInputFields);
        client.commit();
    }
    @Test
    public void searchDco() throws Exception{
        HttpSolrClient client = getClient();
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.set("q","product_catalog_name:生活百货");
        QueryResponse query = client.query(solrQuery);
        SolrDocumentList results = query.getResults();
        System.out.println(results.getNumFound());
        for (SolrDocument result : results) {
            System.out.println(result.getFieldValue("product_catalog_name"));
        }
    }
    @Test
    public void delDoc() throws IOException, SolrServerException {
        HttpSolrClient client = getClient();
        client.deleteByQuery("product_catalog_name:生活百货");
        client.commit();
    }

    @Test
    public void searchHigh()throws Exception{
        HttpSolrClient client = getClient();
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.set("q","product_catalog_name:时尚卫浴 AND product_name:时尚");
        solrQuery.setHighlight(true);
        solrQuery.addHighlightField("product_name");
        solrQuery.addHighlightField("product_catalog_name");
        solrQuery.setHighlightSimplePre("<span>");
        solrQuery.setHighlightSimplePost("</span>");
        solrQuery.setStart(1);
        solrQuery.setRows(1);
        QueryResponse query = client.query(solrQuery);
        SolrDocumentList results = query.getResults();
        System.out.println(results.getNumFound());
        for (SolrDocument result : results) {
            Object id = result.getFieldValue("id");
            Map<String, Map<String, List<String>>> highlighting = query.getHighlighting();
            Map<String, List<String>> id1 = highlighting.get(id);
            List<String> strings = id1.get("product_catalog_name");
            for (String string : strings) {
                System.out.println(string);
            }
            List<String> product_name = id1.get("product_name");
            for (String string : product_name) {
                System.out.println(string);
            }
        }
    }
}
